define(function (require, exports, module) {
    require("apps/process/page.css");
    require("apps/easyui");
    require("apps/file");

    var comp = require("apps/lab/components");
    var filedComponent = comp.components;

    exports.init = function (ctx, uuid) {
        var page = new $.PageDomFactory(uuid);
        page.getJqDom("filed").each(function () {
            var config = $(this).data();
            if (filedComponent[config.field]) {
                var cop = filedComponent[config.field].call(this, ctx, config);
            }
        });

        page.getJqDom("save-btn").linkbutton({
            onClick: function () {
                var postData = {
                    "tProcessTemplate.id": page.getJqDom("processId").val(),
                    tProcessParameters: []
                };
                page.getJqDom("table-form").find("tr.hydra-tr-bg").each(function () {
                    var params = $(this).serializeJson();
                    if(params.fieldType == 'FIELD_TYPE_TABLE'){
                        params.parameterValue = $(this).find("[data-id=f-component]").prop("outerHTML");
                    }
                    postData.tProcessParameters.push(params);
                });
                $("body").wait("start", "模板保存中，请稍后！");
                $.post(ctx + "/app/api/process/addRecord", $.utils.buildJson(postData), function (data) {
                    $("body").wait("end");
                    if ($.isPlainObject(data) && data.error) {
                        $.messager.alert('信息',
                            data.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
//                        window.location.href = WE.contextPath + "/app/api/process/index"
                    }
                }, "JSON")
            }
        });
        page.getJqDom("cancel-btn").linkbutton();
        $.getJSON(ctx + "/app/api/directory/list", {dirGroup: "DIRECTORY_PROCESS_TOOL"}).done(function (data) {
            var content = data.content;
            var options = content.map(function (item) {
                return "<option value=\"" + item.dirValue + "\">" + item.dirValue + "</option>";
            });
            page.getJqDom("device").html(options.join("")).combobox({editable: false});
            page.getJqDom("add-attr").linkbutton("enable");
        });
    }
});